"""
GIST 数据对齐
excel -> folder中的svs
一个病人会对应多个svs
"""
import glob
import os
import pandas as pd
import shutil


# 省医
def sy():
    wsi_dir = '/media/hsmy/wanghao_18T/dataset/20240715-GIST/省医GIST/'
    move_dir = '/media/hsmy/wanghao_18T/dataset/20240715-GIST/省医GIST_other/'
    os.makedirs(move_dir, exist_ok=True)

    excel_path = "/media/hsmy/wanghao_18T/dataset/20240715-GIST/GIST基因队列分组2024.xlsx"
    excel = pd.read_excel(excel_path, sheet_name="省医")
    excel_svs_names = excel.iloc[1:, 0]  # 从第3行开始选取所有行的第0列 第0行是 head
    excel_svs_names = excel_svs_names.astype(str)
    excel_svs_names = excel_svs_names.tolist()

    print(f"excel 中病理号有 : {len(excel_svs_names)} 个")

    listdir = os.listdir(wsi_dir)

    arr = []

    for file in listdir:
        file_name = file[:7]
        arr.append(file_name)
        if file_name not in excel_svs_names:
            # print(f"{file} 不在 excel 中")
            file_path = os.path.join(wsi_dir, file)
            shutil.move(file_path, move_dir)

    miss_arr = []  # excel缺失
    for svs in excel_svs_names:
        if svs not in arr:
            # print(f"{svs} 不在 GIST 文件夹中")
            miss_arr.append(svs)
    pd.DataFrame(miss_arr).to_csv('sy_miss.csv', index=None, header=None)


# 鼓楼医院
def gl():
    wsi_dir = '/media/hsmy/My Book/GIST/鼓楼医院/svs/'
    move_dir = '/media/hsmy/My Book/GIST/鼓楼医院/鼓楼GIST_other/'
    os.makedirs(move_dir, exist_ok=True)

    excel_path = "/media/hsmy/wanghao_18T/dataset/20240715-GIST/GIST基因队列分组2024.xlsx"
    excel = pd.read_excel(excel_path, sheet_name="鼓楼")
    excel_svs_names = excel.iloc[1:, 0]  # 从第3行开始选取所有行的第0列 第0行是 head
    excel_svs_names = excel_svs_names.astype(str)
    excel_svs_names = excel_svs_names.tolist()

    print(f"excel 中病理号有 : {len(excel_svs_names)} 个")

    listdir = os.listdir(wsi_dir)

    arr = []

    for file in listdir:
        file_name = file[:10]
        arr.append(file_name)
        if file_name not in excel_svs_names:
            # print(f"{file} 不在 excel 中")
            file_path = os.path.join(wsi_dir, file)
            shutil.move(file_path, move_dir)

    miss_arr = []  # excel缺失
    for svs in excel_svs_names:
        if svs not in arr:
            # print(f"{svs} 不在 GIST 文件夹中")
            miss_arr.append(svs)
    pd.DataFrame(miss_arr).to_csv('gl_miss.csv', index=None, header=None)


# 南方医
def sm():
    wsi_dir = '/media/hsmy/新加卷/20240724GIST/GIST南方医/'
    move_dir = '/media/hsmy/新加卷/20240724GIST/outer_gist南方医/'
    os.makedirs(move_dir, exist_ok=True)

    excel_path = "/media/hsmy/wanghao_18T/dataset/20240715-GIST/GIST基因队列分组2024.xlsx"
    excel = pd.read_excel(excel_path, sheet_name="smu")
    excel_svs_names = excel.iloc[1:, 0]  # 从第3行开始选取所有行的第0列 第0行是 head
    excel_svs_names = excel_svs_names.astype(str)
    excel_svs_names = excel_svs_names.tolist()

    print(f"excel 中病理号有 : {len(excel_svs_names)} 个")

    listdir = os.listdir(wsi_dir)

    arr = []
    for file in listdir:
        file_name = file.split(".")[0]
        # print(file_name)
        arr.append(file_name)
        if file_name not in excel_svs_names:
            # 将不在excel中的移动到指定文件夹
            file_path = os.path.join(wsi_dir, file)
            shutil.move(file_path, move_dir)

    miss_arr = []
    for svs in excel_svs_names:
        if svs not in arr:
            # 统计缺失的
            miss_arr.append(svs)
    pd.DataFrame(miss_arr).to_csv('smu_miss.csv', index=None, header=None)


# 中六
def z6():
    wsi_dir = '/media/hsmy/新加卷/20240724GIST/GIST中六/wsi/'

    move_dir = '/media/hsmy/新加卷/20240724GIST/outer_gist中六/'
    os.makedirs(move_dir, exist_ok=True)

    excel_path = "/media/hsmy/wanghao_18T/dataset/20240715-GIST/GIST基因队列分组2024.xlsx"
    excel = pd.read_excel(excel_path, sheet_name="中六")
    excel_svs_names = excel.iloc[1:, 0]
    excel_svs_names = excel_svs_names.astype(str)
    excel_svs_names = excel_svs_names.tolist()

    print(f"excel 中病理号有 : {len(excel_svs_names)} 个")

    listdir = os.listdir(wsi_dir)

    arr = []
    for file in listdir:
        file_name = file[:8]
        print(file_name)
        arr.append(file_name)
        if file_name not in excel_svs_names:
            # 将不在excel中的移动到指定文件夹
            file_path = os.path.join(wsi_dir, file)
            shutil.move(file_path, move_dir)

    miss_arr = []
    for svs in excel_svs_names:
        if svs not in arr:
            # 统计缺失的
            miss_arr.append(svs)
    pd.DataFrame(miss_arr).to_csv('z6_miss.csv', index=None, header=None)

# 通过后缀移动文件
def move_by_pattern():
    wsi_path = "/media/hsmy/新加卷/20240724GIST/GIST中六/wsi/"
    target_dir = "/media/hsmy/新加卷/20240724GIST/outer_gist中六/"
    os.makedirs(target_dir, exist_ok=True)
    for file_path in glob.glob(os.path.join(wsi_path, '*.ndpi')):
        # print(file_path)
        shutil.move(file_path, target_dir)

def move_by_pattern2():
    wsi_path = "/media/hsmy/新加卷/20240724GIST/GIST中六/wsi/"
    xml_path = "/media/hsmy/新加卷/20240724GIST/GIST中六/xml/"
    target_dir = "/media/hsmy/新加卷/20240724GIST/GIST中六/"

    for file in os.listdir(wsi_path):
        file_name = file.split(".")[0]
        file_path = os.path.join(xml_path, file_name + ".xml")
        # print(file_path)
        shutil.move(file_path, target_dir)

if __name__ == '__main__':
    move_by_pattern2()
